#include <caintplanner/CAIntPlannerE.h>

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



	// CONSTRUCTORES***********************************************

CAIntPlanner::EDROOMTopContext::EDROOMTopContext (CAIntPlanner &act, CEDROOMPOOLCDCommConfig & EDROOMpPoolCDCommConfig ):

	EDROOMcomponent(act)
	,Msg(EDROOMcomponent.Msg)
	,MsgBack(EDROOMcomponent.MsgBack)
	,SSICtrl(EDROOMcomponent.SSICtrl)
	,EmuPlannerSGIControl(EDROOMcomponent.EmuPlannerSGIControl)
	, CEDROOMPoolCDCommConfig ( EDROOMpPoolCDCommConfig )
 {

}

CAIntPlanner::EDROOMTopContext::EDROOMTopContext ( EDROOMTopContext &contex ):

	EDROOMcomponent(contex.EDROOMcomponent),
	Msg(contex.Msg),
	MsgBack(contex.MsgBack)
	,SSICtrl(contex.SSICtrl)
	,EmuPlannerSGIControl(contex.EmuPlannerSGIControl)
	, CEDROOMPoolCDCommConfig ( contex.CEDROOMPoolCDCommConfig )
 {

}

	// EDROOMSearchContextTrans********************************************

bool CAIntPlanner::EDROOMTopContext::EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans){

	bool edroomValidMsg=false; 

	 switch(Msg->signal){

		 case ( EDROOMSignalDestroy ): 

			 edroomValidMsg=true;
			 edroomCurrentTrans.distanceToContext = 0 ;
			 edroomCurrentTrans.localId = -1 ;
			 break;

	}

	return(edroomValidMsg);

}

	// Funciones Definidas por el usuario  ****************************

void	CAIntPlanner::EDROOMTopContext::FInit()

{
	// send new Msg
	SSICtrl.send(SToListening, EDROOMprioNormal);
 
}



void	CAIntPlanner::EDROOMTopContext::FSendKeepOnComm()

{
	
		CDCommConfig * ap_CDCommConfig = CEDROOMPoolCDCommConfig.AllocData();
 
		// Complete Data
 
		// invoke
 
		MsgBack=EmuPlannerSGIControl.invoke(SKeepOnComm, EDROOMprioVeryHigh, ap_CDCommConfig, &CEDROOMPoolCDCommConfig);
 
		// Handling MsgBack
 
		// End Handling MsgBack
 
		EDROOMFreeMsgBack(MsgBack);
	
 
}



void	CAIntPlanner::EDROOMTopContext::FSendToSgi()

{
	printf("Comienza el pase de HumSAT-UAH\n");
	CDCommConfig * ap_CDCommConfig = CEDROOMPoolCDCommConfig.AllocData();
	// Complete Data
	//ap_CDCommConfig
 
	// invoke
	MsgBack=EmuPlannerSGIControl.invoke(SToComm, EDROOMprioNormal, ap_CDCommConfig, &CEDROOMPoolCDCommConfig);
	
	// Handling MsgBack
	// End Handling MsgBack
	EDROOMFreeMsgBack(MsgBack);
}



void	CAIntPlanner::EDROOMTopContext::FTimerEmuPlanner()

{
#ifdef CONFIG_DEMO_SAF_IT_SWR_DEMO
 	Pr_Time	interval(30.0); // Declare time var with an initial value in seconds
#else
 	Pr_Time	interval(2.0); // Declare time var with an initial value in seconds
#endif
 
	Timer.InformIn( interval, EDROOMprioNormal );
}



	//*****Pools

	//CEDROOMPOOLCDCommConfig

CAIntPlanner::EDROOMTopContext::CEDROOMPOOLCDCommConfig::CEDROOMPOOLCDCommConfig(unsigned int elemCount, CDCommConfig* pMem, TEDROOMBool * pMemMarks):CEDROOMProtectedMemoryPool(elemCount, pMem, pMemMarks, sizeof (CDCommConfig)){;}

CDCommConfig *	CAIntPlanner::EDROOMTopContext::CEDROOMPOOLCDCommConfig::AllocData(){
	return(CDCommConfig*)CEDROOMProtectedMemoryPool::AllocData();
}



// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



	// CONSTRUCTOR*************************************************

CAIntPlanner::EDROOMTopState::EDROOMTopState (CAIntPlanner &act, CEDROOMMemory *pEDROOMMemory  ):
	  EDROOMTopContext( act ,CEDROOMPoolCDCommConfig )
	, CEDROOMPoolCDCommConfig ( 10, pEDROOMMemory->poolCDCommConfig, pEDROOMMemory->poolMarkCDCommConfig)
{

}

	// EDROOMBehaviour**********************************************

void CAIntPlanner::EDROOMTopState::EDROOMBehaviour(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans = EDROOMIllegada();

	do{

		switch(edroomCurrentTrans.localId){

			case(Init):
				
				edroomNextState = Ready;
				break;
		}

		EDROOMcomponent.EDROOMTraceStateEntry();

		EDROOMcomponent.EDROOMTraceStateEntry();

		switch(edroomNextState){

			case(I):
				edroomCurrentTrans=EDROOMIllegada();
				break;

			case(Ready):
				FTimerEmuPlanner();
				edroomCurrentTrans=EDROOMReadyllegada();
				break;

		}

		EDROOMcomponent.EDROOMTraceStateExit();

		edroomCurrentState=edroomNextState;

	}while(Msg->signal != EDROOMSignalDestroy);

}



	// Inicio**********************************************

void CAIntPlanner::EDROOMTopState::EDROOMInicio(){

edroomCurrentState=I;

}



//	 ***********************************************************************

//	 llegada SubEstado I

//	 ***********************************************************************



TEDROOMTransId CAIntPlanner::EDROOMTopState::EDROOMIllegada(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans.localId = Init;
	edroomCurrentTrans.distanceToContext = 0;
	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, 0);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja Ready

	// ***********************************************************************



TEDROOMTransId CAIntPlanner::EDROOMTopState::EDROOMReadyllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



